package test.hxy.mongoSchoolSimple.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.hxy.mongoSchoolSimple.dao.CampusTenplateDao;
import test.hxy.mongoSchoolSimple.dao.TeacherDao;
import test.hxy.mongoSchoolSimple.dao.TeacherTenplateDao;
import test.hxy.mongoSchoolSimple.model.Teacher;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    TeacherTenplateDao teacherTenplateDao;
    @Autowired
    CampusTenplateDao campusTenplateDao;
    public List<Teacher> findAll(){
        return teacherDao.findAll();
    }

    public List<Teacher> findByTeacherNameContains(String teacherName) {
        return teacherDao.findByTeacherNameContains(teacherName);
    }

    public Teacher findByTeacherIdEquals(String teacherId) {
        return teacherDao.findByTeacherIdEquals(teacherId);
    }

    public List<Teacher> findByCampusIdEquals(String campusId) {
        return teacherDao.findByCampusIdEquals(campusId);
    }

    public Teacher insertTeacher(Teacher teacher) {
        teacher.set_id(teacher.getTeacherId());
        Teacher temp = teacherDao.findByTeacherIdEquals(teacher.getTeacherId());
        if (teacher.isPrincipal()) {
            teacherTenplateDao.cancelPrincipal(teacher.getCampusId());
            campusTenplateDao.updatePrincipal(teacher.getCampusId(), teacher.getTeacherName());
        }
        if (temp == null) {
            teacherTenplateDao.insert(teacher);
            return teacher;
        } else {
            return null;
        }
    }

    public Teacher delete(String teacherId) {
        Teacher temp = teacherDao.findByTeacherIdEquals(teacherId);
        if (temp != null) {
            teacherTenplateDao.delete(teacherId);
            return temp;
        } else {
            return null;
        }
    }
}
